using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;
using TitanX;
using System;

public enum PlayerStart {stand, run,attack}
                    //   手   刀  弓
public enum PlayerWeapon {hand, knife,bow}



//玩家UI
public class PlayerUI{
    public GameObject[] booldView;

}



public class Player : SingletonGetMono<Player>
{



    public GameObject weaponGM;
    
    [Header("闪避率")]
    public float missOdds;

    //可走的格子
  //  public GameObject preBox;

    //需要闪烁的
    public Material material;

    //玩家本身
    public GameObject player;

    public PlayerUI playerUI;

    //当前被锁定的敌人
    public GameObject LockedEmeny;

    public GameObject TempEmeny;
   
    //玩家状态
    public   PlayerStart playerStart;

    //移动到的位置坐标
    public Vector2 moveVector2;


    //HP的UI显示
    public GameObject[] hpView;
    public float maxHp=3;

    //public Slider hpSlider;
    public Image hpImage;
    public Image huangchongHp;



    //血量
    public int blood;

    // Start is called before the first frame update
    void Start()
    {


        player = gameObject;
      //  preBox = transform.Find("PreBox").gameObject;
    }

    // Update is called once per frame
    void Update()
    {
       //移动
        if (playerStart == PlayerStart.run) {
            gameObject.transform.DOLocalMove(moveVector2,0.3f).OnComplete(delegate {
                    


                    //移动完成之后，减少点数
                    if (playerStart == PlayerStart.run)
                    {
                        //将玩家切换回去之前的状态，继续行动
                        playerStart = PlayerStart.stand;
                        Choose();
                    }
            });

        }



        
    }



    public void ReduceHP(int i){
        float hou = 2f;// hpImage.gameObject.GetComponent<RectTransform>().localScale.x - (1 / 6);
        float hou2 = huangchongHp.gameObject.GetComponent<RectTransform>().localScale.x - (1 / 6);


        ViewManger.Instance.showTip("-2", gameObject.transform, 1f);

        //开启携程，白光效果
        StartCoroutine(Injurd());


            if(blood>0){
            
                while(i>0){
                    //为啥要怎么写呢，方便更新血量的UI界面
                    i--;
                    blood--;
                //更新血量UI；
                //血条的血量是被直接算出来的长度大小
                //是一套计算
                //内框的大小计算

                // hpView[blood].GetComponent<Image>().DOFade(0,0.3f);

                //血量更换为Slider 
                //一个算法，当前玩家的最大血量，等分，将1等分

                /// 1除以n
                //float reduceWidth=1/maxHp;
                //就是怎么简单，，，，
                //    hpSlider.value-=reduceWidth;


                 //  Debug.Log("被攻击后减血"+ hpImage.gameObject.GetComponent<RectTransform>().localScale.x + (1 / 6));
//
                 
                 //   hpImage.gameObject.GetComponent<RectTransform>().DOScaleX(hou, 0.1f).SetEase(Ease.InExpo);
                 //   huangchongHp.gameObject.GetComponent<RectTransform>().DOScaleX(hou2, 0.2f);
                   


                
                }

            }else{
                //Dead();
                //执行死亡事件
            }

    }



    //移动到指定位置
    public void Move(Vector2 v2)
    {
        PlayerWaitBoxManger.Instance.Hide();
        moveVector2 = v2;
        playerStart = PlayerStart.run;
    }


    //攻击
    public void Attack(GameObject emeny)
    {
        //抬起武器，看准时机，暴击
        //播放抬武器的 动画
       
        //位移给取消掉
        //位移的是拳头                                武器  和   敌人的偏移量  乘 一个位数
         Vector2 swing= weaponGM.transform.position+(weaponGM.transform.position-emeny.transform.position)*0.2f;
         Vector2 xSwing= weaponGM.transform.position-(weaponGM.transform.position-emeny.transform.position)*0.2f;

        //  float swing= weaponGM.transform.position.x + 0.2f;

        //  float xSwing = weaponGM.transform.position.x - 0.03f;
        Vector2 yuanSwing = weaponGM.transform.position;




        print("位移hou："+swing+"qian:"+xSwing);
        Tweener tweener = weaponGM.transform.DOMove(swing,0.05f);
        Tweener tweener0 =weaponGM.transform.DOMove(xSwing,0.3f);
        Tweener tweener2 =weaponGM.transform.DOMove(yuanSwing,0.1f);
        Sequence quence = DOTween.Sequence();

        quence.Append(tweener0);
        quence.Append(tweener);
        quence.Append(tweener2);

        //似乎也美誉啥问题啊，好像不能返回到，初始的状态
      //  tweener.SetLoops(2, LoopType.Yoyo);
        quence.Play();

        quence.OnComplete(delegate
        {
            quence.Kill();
            weaponGM.SetActive(false);
        });

       // preBox.SetActive(false);

        PlayerWaitBoxManger.Instance.Show();

        playerStart = PlayerStart.attack;
        //还可以继续攻击
        //见血

        //像，emeny方向偏移一下下，立刻回来
        /*
        Vector2 swing2= player.transform.position+(player.transform.position-emeny.transform.position)*0.3f;
        print("位移："+swing);
        Tweener tweener2 =  player.transform.DOLocalMove(swing2,0.3f);
        tweener2.SetLoops(2, LoopType.Yoyo);
        tweener2.Play();
        */
        emeny.GetComponent<FSM>().reduceHP(1);

        //减血，玩家恢复到初始状态
        Choose();

       
        //一段小动画，+sprite闪烁
        //敌人减血

    }


    //受伤，效果
    IEnumerator Injurd()
    {
        yield return new WaitForSeconds(0.1f);
        material.SetFloat("_FlashAmount", 1f);
        yield return new WaitForSeconds(0.02f);
        material.SetFloat("_FlashAmount", 0f);
        Debug.Log("开启减血的携程");
        hpImage.gameObject.GetComponent<RectTransform>().DOScaleX(hpImage.gameObject.GetComponent<RectTransform>().localScale.x - (1f / 6), 0.1f).SetEase(Ease.InExpo);
        huangchongHp.gameObject.GetComponent<RectTransform>().DOScaleX(huangchongHp.gameObject.GetComponent<RectTransform>().localScale.x - (1f / 6), 0.2f).SetEase(Ease.InExpo);
                   


    }




    //进入选择状态
     public  void Choose()
    {

        Debug.Log("重新进入选择模式下");

            //减少Gm的点数
            GameManger.Instance.reducePoint();
        //减少点数，会进行判断要进入到什么样的状态
        //玩家的状态
        
        if (GameManger.Instance.round==GameManger.Round.player){
                playerStart = PlayerStart.stand;
                //GameManger全局的状态 - 选择
                GameManger.Instance.handleStart = GameManger.HandleStart.choose;
            PlayerWaitBoxManger.Instance.Show();

            Controller.Instance.clear();
            }            

    
    }


    

}
